package com.rms.hkpc.audio;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.rms.hkpc.R;


public class RecordVoicePopWindow extends PopupWindow {

    private TextView item_tv_status, item_tv_time;
    private ImageView item_iv_status, item_iv_volume;


    public RecordVoicePopWindow(Context context) {
        super(context);
        View view = LayoutInflater.from(context).inflate(R.layout.pop_record_voice, null);
        setContentView(view);
        item_tv_status = view.findViewById(R.id.item_tv_status);
        item_iv_status = view.findViewById(R.id.item_iv_status);
        item_iv_volume = view.findViewById(R.id.item_iv_volume);
        item_tv_time = view.findViewById(R.id.item_tv_time);
        // 设置弹窗的宽
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置弹窗的高
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置弹窗可点击
        setFocusable(true);
        setOutsideTouchable(false);
        setTouchable(false);
        // 设置弹窗的背景
        setBackgroundDrawable(new ColorDrawable(0x00000000));
    }

    /**
     * 正常录制
     */
    public void showRecordingTipView() {
        item_iv_status.setVisibility(View.VISIBLE);
        item_iv_status.setImageResource(R.mipmap.record_volume_microphone);
        item_iv_volume.setVisibility(View.VISIBLE);
        item_iv_volume.setImageResource(R.mipmap.record_volume_01);
        item_tv_status.setVisibility(View.VISIBLE);
        item_tv_status.setText(R.string.remove_above_cancel_send);
        item_tv_time.setVisibility(View.GONE);
    }

    /**
     * 即将超时
     *
     * @param remainder
     */
    public void showTimeOutTipView(int remainder) {
        item_iv_status.setVisibility(View.GONE);
        item_iv_volume.setVisibility(View.GONE);
        item_tv_status.setVisibility(View.VISIBLE);
        item_tv_status.setText(R.string.remove_above_cancel_send);
        item_tv_time.setText(String.format("%s", remainder));
        item_tv_time.setVisibility(View.VISIBLE);
    }

    /**
     * 录制时间太短
     */
    public void showRecordTooShortTipView() {
        item_iv_status.setVisibility(View.VISIBLE);
        item_iv_volume.setVisibility(View.GONE);
        item_iv_status.setImageResource(R.mipmap.record_volume_warning);
        item_tv_status.setText(R.string.rec_voice_short);
    }

    /**
     * 松开手指，取消发送
     */
    public void showCancelTipView() {
        item_iv_status.setVisibility(View.VISIBLE);
        item_iv_volume.setVisibility(View.GONE);
        item_iv_status.setImageResource(R.mipmap.record_volume_cancel);
        item_tv_status.setVisibility(View.VISIBLE);
        item_tv_status.setText(R.string.loosen_cancel_send);
        item_tv_time.setVisibility(View.GONE);
    }

    /**
     * 更新当前音量大小
     */
    public void updateCurrentVolume(int decibel) {
        switch (decibel / 5) {
            case 0:
            case 1:
                item_iv_volume.setImageResource(R.mipmap.record_volume_01);
                break;
            case 2:
                item_iv_volume.setImageResource(R.mipmap.record_volume_02);
                break;
            case 3:
                item_iv_volume.setImageResource(R.mipmap.record_volume_03);
                break;
            case 4:
                item_iv_volume.setImageResource(R.mipmap.record_volume_04);
                break;
            case 5:
                item_iv_volume.setImageResource(R.mipmap.record_volume_05);
                break;
            default:
                item_iv_volume.setImageResource(R.mipmap.record_volume_06);
        }
    }

}
